using System.Linq;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [Header("Movement Settings")]
    public float moveForce = 10f; // 移动力
    public float jumpForce = 7f;  // 跳跃力
    public float maxSpeed = 5f;   // 最大速度

    [Header("Ground Check")]
    public Transform groundCheck; // 地面检测点的Transform
    public float checkRadius = 0.1f; // 检测半径
    public LayerMask whatIsGround; // 指定哪些层是地面

    private Rigidbody rb;
    private bool isGrounded;

    [Header("Roll/Dash")]
    public float dashForce = 15f;
    public float dashMaxSpeed = 10f;
    private bool isDashing;

    [Header("Grab")]
    public Transform grabPoint; // 抓取点的位置
    public float grabRange = 2f;
    private GameObject grabbedObject;
    private bool isGrabbing;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        this.groundCheck = transform.Find("GroundCheck");
        this.grabbedObject = transform.Find("GrabPoint").gameObject;
        Debug.Log(this.grabbedObject);
        Debug.Log(this.groundCheck);
    }

    void Update()
    {
        SystemSetting.Quit();

        // 检测是否在地面
        isGrounded = Physics.CheckSphere(groundCheck.position, checkRadius, whatIsGround);

        // 跳跃输入
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            Jump();
        }

        // 冲刺输入
        isDashing = Input.GetKey(KeyCode.LeftShift);

        // 抓取/释放输入
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (!isGrabbing)
            {
                TryGrab();
            }
            else
            {
                Release();
            }
        }
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);

        float currentMoveForce = isDashing ? dashForce : moveForce;
        float currentMaxSpeed = isDashing ? dashMaxSpeed : maxSpeed;

        if (rb.velocity.magnitude < currentMaxSpeed)
        {
            rb.AddForce(movement * currentMoveForce);
        }
    }

    void Jump()
    {
        // 重置Y轴速度，防止在空中连续跳跃更强
        rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
        rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    }

    // 在Scene视图中绘制地面检测Gizmo，便于调试
    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(groundCheck.position, checkRadius);
    }

    void TryGrab()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, out hit, grabRange))
        {
            // 假设所有有Rigidbody的物体都能被抓取
            Rigidbody hitRigidbody = hit.collider.GetComponent<Rigidbody>();
            if (hitRigidbody != null)
            {
                grabbedObject = hit.rigidbody.gameObject;
                // 取消被抓取物体的物理模拟，使其跟随玩家
                hitRigidbody.isKinematic = true;
                // 将其设置为抓取点的子物体
                grabbedObject.transform.SetParent(grabPoint);
                grabbedObject.transform.localPosition = Vector3.zero; // 放到抓取点中心
                isGrabbing = true;
            }
        }
    }

    void Release()
    {
        if (grabbedObject != null)
        {
            Rigidbody rb = grabbedObject.GetComponent<Rigidbody>();
            rb.isKinematic = false;
            grabbedObject.transform.SetParent(null);
            // 可以给一个向前的力来投掷
            rb.AddForce(transform.forward * 10f, ForceMode.Impulse);
            grabbedObject = null;
            isGrabbing = false;
        }
    }
}